get.dcov.vars = function(model,p.vars,k.minus.p.vars,rank=FALSE){
	#apply dcov method	
	dcov.vars=p.vars
	#try to find variables that might need to be included that were previously discarded as redundant
	for(j in k.minus.p.vars){
		if(rank==FALSE){
			dcov.test.result = dcov.test(	model[,j],	model[,dcov.vars],R=199) 
		}else{	
			dcov.test.result = rank.dcov.test(	model[,j],	model[,dcov.vars]) 			
		}
			distance.covar.p.value = as.numeric(dcov.test.result$p.value);
		if(distance.covar.p.value>.05){
			#independent variable,so keep it
			dcov.vars=c(dcov.vars,j)
		}	
	}
	dcov.vars=dcov.vars[rank(dcov.vars)]

	#try to discard redundant variables in our selection
		redundancy.matrix=as.data.frame(as.matrix(t(rep(0,length(dcov.vars)))))

		for (j in 1:(length(dcov.vars)-1)){
			redundancy.matrix=rbind(redundancy.matrix,as.matrix(t(rep(0,length(dcov.vars)))))
		}	
		names(redundancy.matrix)=dcov.vars
		row.names(redundancy.matrix)=dcov.vars
		for(j in dcov.vars){

			for(k in dcov.vars[which(dcov.vars>j)]){
				if(rank==FALSE){
					dcov.test.result = dcov.test(	model[,j],	model[,k],R=199) 
				}else{	
					dcov.test.result = rank.dcov.test(	model[,j],	model[,k]) 			
				}
				distance.covar.p.value = as.numeric(dcov.test.result$p.value);
				if(distance.covar.p.value<.05){
					#dependent variable,so discard it
					redundancy.matrix[which(names(redundancy.matrix)==k),which(names(redundancy.matrix)==j)]=1
					redundancy.matrix[which(names(redundancy.matrix)==j),which(names(redundancy.matrix)==k)]=1
				}	
			}
		}
			#discard the highest-ranking redundancies
			#determine redundancy scores from redundancy score matrix
			dcov.vars.temp=c(0)
	while(length(dcov.vars.temp)!=length(dcov.vars)){
			#ensures all maximum redundancies are taken-out
			dcov.vars.temp=dcov.vars
			#create the redundancy score, which is just a column sum
			redundancy.score=as.data.frame(as.matrix(t(rep(0,length(dcov.vars)))))
			names(redundancy.score)=dcov.vars
			for(j in names(redundancy.score)){
			
				redundancy.score[,which(names(redundancy.score)==j)]=sum(redundancy.matrix[,which(names(redundancy.matrix)==j)])
			}
			variable.discarded = as.numeric(names(which.max(redundancy.score)))
			
			if(redundancy.score[,which(names(redundancy.score)==variable.discarded)]==0){
				#do nothing, this is not 'redundant'
				
			}else{
				#non-zero, so remove the edge from the matrix and discard
				for(j in as.numeric(names(redundancy.matrix)[which(names(redundancy.matrix)!=variable.discarded)])){
					
					if(redundancy.matrix[which(names(redundancy.matrix)==variable.discarded),which(names(redundancy.matrix)==j)]>0){
						#dependent variable,so discard it
						redundancy.matrix[which(names(redundancy.matrix)==variable.discarded),which(names(redundancy.matrix)==j)]=
							redundancy.matrix[which(names(redundancy.matrix)==variable.discarded),which(names(redundancy.matrix)==j)]-1;
						redundancy.matrix[which(names(redundancy.matrix)==j),which(names(redundancy.matrix)==variable.discarded)]=
							redundancy.matrix[which(names(redundancy.matrix)==j),which(names(redundancy.matrix)==variable.discarded)]-1;
					}	
				}
				dcov.vars=dcov.vars[which(dcov.vars!=variable.discarded)]			
				dcov.vars=dcov.vars[rank(dcov.vars)]
			}				
				
	}
     	return(structure(list(dcov.vars = dcov.vars)));
}

